<?php
	// Pull in includes file
	require('system/includes.php');
	require('system/softlinks.php');

	// Get URL, parse, clean and set to array
	//$uri = substr($_SERVER['REQUEST_URI'], 1); // remove the first slash
	//$uri = str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri); // remove the query string

	$uri_array = parse_url($_SERVER['REQUEST_URI']);
	$uri = trim($uri_array['path'], "/");
	//print_r($uri);

	//right, check to see if the $_SERVER['REQUEST_URI'] exists in the $softlinks_array and if so then load that in
	$softlink_found = recursiveArraySearch($softlinks_array, $uri, 'softlink');
	if(is_numeric($softlink_found))
	{
		//right, softlink found, we need to load in the "redirect_to" value and load in that page
		$uri = $softlinks_array[$softlink_found]['redirect_to'];
	}
	$segments = explode('/', $uri);

	//print_r($segments);

	// Create controller/action paths to pull in require view
	$controller = strlen($segments[0]) > 0 ? array_shift($segments) : 'home';
	$action = strlen(!empty($segments[0])) > 0 ? array_shift($segments) : 'index';
	$args = $segments;
	$controllerPath = SYSTEM_CONTROLLER . $controller . '/' . $action . '.php';

	// If view exists, output, else return 404 page
	if(file_exists($controllerPath)) {
		require($controllerPath);
	} else {
		header("HTTP/1.0 404 Not Found");
		require_once(SYSTEM_CONTROLLER.'errors/404.php');
}
?>